package com.iwop.moto.utils.LotteryUtils;

import com.iwop.moto.comm.ConfLottery;

import java.util.*;

/**
 * 抽奖概率核心类
 * #author chenzq
 * #create 2017-01-10-16:07
 */
public class CouponLotteryUtil {


    //私有构造 防止实例化
    private CouponLotteryUtil() {
    }

    //单例
    private static CouponLotteryUtil instance;

    public static CouponLotteryUtil getInstance() {
        if (instance == null) {
            instance = new CouponLotteryUtil();
        }
        return instance;
    }


    /**
     * 随机出金额区间的所得优惠券id
     *
     * @param money 金额
     * @param map   抽奖配置
     * @return 优惠券id集合
     */
    public static List<String> fuckYouCoupon(Double money, Map map) {
        List<String> list = null;
        int rundom = getRundom(99, 0);
        Iterator<Map.Entry<Double[], Map<Integer[], String[]>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Double[], Map<Integer[], String[]>> next = iterator.next();
            Double[] key = next.getKey();
            if (key[0] <= money && money <= key[1]) {
                Map<Integer[], String[]> value = next.getValue();
                Iterator<Map.Entry<Integer[], String[]>> iterator1 = value.entrySet().iterator();
                while (iterator1.hasNext()) {
                    Map.Entry<Integer[], String[]> next1 = iterator1.next();
                    Integer[] key1 = next1.getKey();
                    if (key1[0] <= rundom && rundom < key1[1]) {
                        String[] value1 = next1.getValue();
                        list = Arrays.asList(value1);
                    }
                }
            }
        }
        return list;
    }


    /**
     * 用户端 概率
     *
     * @return
     */
    public static Map<Double[]/*金额区间*/, Map<Integer[]/*中奖概率区间*/, String[]/*优惠券列表*/>> initUserMap() {
        Map<Double[], Map<Integer[], String[]>> version8 = UserCouponVersions.version8();
        return version8;
    }

    /**
     * 商家端 概率
     *
     * @return
     */
    public static Map<Double[]/*金额区间*/, Map<Integer[]/*中奖概率区间*/, String[]/*优惠券列表*/>> initBusinessMap() {
        Map<Double[], Map<Integer[], String[]>> version5 = BusinessCounponVersions.version5();
        return version5;
    }

    /**
     * 获取随机数
     *
     * @param max 最大值
     * @param min 最小值
     * @return
     */
    private static int getRundom(int max, int min) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    public static void main(String[] args) {

    }
}
